package com.EVO.controller;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class Servlet2 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int zou = 20;
        int ca = 15;
        int ba = 98;
        int money = 0;
        int xiaofei = 0;
        Cookie[] cookieArray = null;
        Cookie newCard = null;
        String food,userName;
        userName = null;
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        food = request.getParameter("food");
        cookieArray = request.getCookies();
        for(Cookie cookie:cookieArray){
            String key = cookie.getName();
            String value = cookie.getValue();
            if("userName".equals(key)){
                userName = value;
            }else if("money".equals(key)){
                money = Integer.valueOf(value);
                if("典明粥".equals(food)){
                    if(zou > money){
                        out.print("余额不足请充值");
                    }else{
                        newCard = new Cookie("money",(money-zou)+"");
                        xiaofei = zou;
                    }
                }else if("阿帕茶".equals(food)){
                    if(ca > money){
                        out.print("余额不足请充值");
                    }else{
                        newCard = new Cookie("money",(money-ca)+"");
                        xiaofei = ca;
                    }
                }else if("粑粑".equals(food)){
                    if(ba > money){
                        out.print("余额不足请充值");
                    }else{
                        newCard = new Cookie("money",(money-ba)+"");
                        xiaofei = ba;
                    }
                }
            }

        }

        response.addCookie(newCard);
        out.print("用户："+userName+"本次消费："+xiaofei+"余额：");
    }
}
